﻿
/// <reference path="vswd-ext_2.2.js" />
/********************poi.js*********************************
*Create Date: 2012-2
*Description : For record the relation  between tracker and polygon geofence
*********************************************************************/

function TrackerPolygonFenceWindow() {

    if (Ext.getCmp('tracker_polygon_fence_relation_window')) {
        Ext.getCmp('tracker_polygon_fence_relation_window').show();
        return;
    }
    
    var pgeofencefields = ["tracker_polygon_fence_relation_id", "sn_imei_id", "tracker_name", "polygon_geofence_id", "alarm_type", "points", "name"];
    var pgeofencestore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchTrackerPolygonFence",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: pgeofencefields,
            root: "data"
        }),
        autoLoad: true
    });

    var trackNameCombo =  getTrackerCheckboxCombo(false);
    var tbar = new Ext.Toolbar({
        items: [
            trackNameCombo, {
                xtype: 'tbseparator', width: 5
            }, {
                tooltip: { text: _TxtShowAllPolygonFenceByTrackerName, autoHide: true },
                icon: 'images/view.gif',
                handler: function() {
                    var count = grid.getStore().getCount();
                    for (var i = 0; i < count; i++) {
                        var sn = trackNameCombo.getValue();
                        var record = grid.getStore().getAt(i);
                        var sn_imei_id = record.get('sn_imei_id');
                        if (sn_imei_id == trackNameCombo.getValue()) {
                            var iiid = record.get('polygon_geofence_id');
                            imap.showCannotEditPolygonFence(record.get('points'), iiid, true, record.get('name'));
                        }
                    }
                }
            }, {
                xtype: 'tbseparator', width: 5
            }, {

                icon: 'images/add.png',
                handler: function() {
                    var pgeofencewindow = Ext.getCmp('add_tracker_polygon_fence_relation_window');
                    if (pgeofencewindow == null) {
                        var trackerCombo = getTrackerCheckboxCombo(true);
                        var fenceCombo = getFenceCheckboxCombo(true);
                        var myCheckboxGroup = new Ext.form.CheckboxGroup({
                            id: 'alarmTypeGroup',
                            xtype: 'checkboxgroup',
                            fieldLabel: _Alarm_Header_AlarmType,
                            itemCls: 'x-check-group-alt',
                            columns: 1,
                            items: [{
                                boxLabel: _TxtInAlarm,
                                name: 'inalarm'
                            }, {
                                boxLabel: _TxtOutAlarm,
                                name: 'outalarm'
                            }
    ]
                        });

                        var form = new Ext.FormPanel({
                            width: 375,
                            autoHeight: true,
                            plain: true,
                            layout: "form",
                            closeAction: 'hide',
                            defaultType: "textfield",
                            labelWidth: 100,
                            plain: true,
                            baseCls: "x-plain",
                            defaults: { anchor: "95%", msgTarget: "side" },
                            buttonAlign: "center",
                            bodyStyle: "padding:0 0 0 0",
                            items: [trackerCombo, fenceCombo, myCheckboxGroup]
                        });

                        pgeofencewindow = new Ext.Window({
                            title: _CustomerConfigResource_AddNew,
                            id: 'add_tracker_polygon_fence_relation_window',
                            layout: 'fit',
                            height: 250,
                            width: 375,
                            items: form,
                            bodyStyle: "padding:5 5 5 5",
                            resizable: false,
                            modal: true,
                            buttons: [{
                                text: _CustomerConfigResource_Save,
                                handler: function() {
                                    if (form.getForm().isValid()) {
                                        Ext.MessageBox.show({
                                            msg: _CustomerConfigResource_Saving + "...",
                                            progressText: _CustomerConfigResource_Saving + "...",
                                            width: 300,
                                            wait: true,
                                            waitConfig: { interval: 200 },
                                            icon: 'download',
                                            animEl: 'saving'
                                        });
                                        form.getForm().submit({
                                            url: "HandlerADUTrackerPolygonFence",
                                            method: "POST",
                                            success: function(form, action) {
                                                var flag = action.result.success;
                                                if (flag == true) {
                                                    Ext.MessageBox.hide();
                                                    msgUpdateSuccess();
                                                    pgeofencewindow.hide();
                                                    pgeofencestore.reload({
                                                        params: {
                                                            namekey: ''
                                                        }
                                                    });
                                                }
                                            },
                                            failure: function(form, action) {
                                                msgUpdateFailure();
                                            },
                                            params: {
                                                snids: trackerCombo.getValue(),
                                                fenceids: fenceCombo.getValue(),
                                                type: 'add'
                                            }
                                        });
                                    }
                                }
}]
                            });
                        }
                        pgeofencewindow.show();
                    }
                }, {
                    xtype: 'tbseparator', width: 5
                }, {

                    icon: 'images/edit.gif',
                    handler: function() {
                        if (grid.getSelectionModel().getCount() == 1) {
                            var pgeofencewindow = Ext.getCmp('edit_tracker_polygon_fence_relation_window');
                            if (pgeofencewindow == null) {
                                var trackerCombo = getTrackerCheckboxCombo(false);
                                var fenceCombo = getFenceCheckboxCombo(false);
                                var myCheckboxGroup = new Ext.form.CheckboxGroup({
                                    id: 'editAlarmTypeGroup',
                                    xtype: 'checkboxgroup',
                                    fieldLabel: _Alarm_Header_AlarmType,
                                    itemCls: 'x-check-group-alt',
                                    columns: 1,
                                    items: [{
                                        boxLabel: _TxtInAlarm,
                                        name: 'inalarm'
                                    }, {
                                        boxLabel: _TxtOutAlarm,
                                        name: 'outalarm'
                                    }
    ]
                                });
                                var form = new Ext.FormPanel({
                                    width: 375,
                                    autoHeight: true,
                                    plain: true,
                                    layout: "form",
                                    closeAction: 'hide',
                                    defaultType: "textfield",
                                    labelWidth: 100,
                                    plain: true,
                                    baseCls: "x-plain",
                                    defaults: { anchor: "95%", msgTarget: "side" },
                                    buttonAlign: "center",
                                    bodyStyle: "padding:0 0 0 0",
                                    items: [trackerCombo, fenceCombo, myCheckboxGroup]
                                });

                                pgeofencewindow = new Ext.Window({
                                    title: _TxtEdit,
                                    id: 'edit_tracker_polygon_fence_relation_window',
                                    layout: 'fit',
                                    height: 250,
                                    width: 375,
                                    items: form,
                                    bodyStyle: "padding:5 5 5 5",
                                    resizable: false,
                                    modal: true,
                                    buttons: [{
                                        text: _CustomerConfigResource_Save,
                                        handler: function() {
                                            if (form.getForm().isValid()) {
                                                Ext.MessageBox.show({
                                                    msg: _CustomerConfigResource_Saving + "...",
                                                    progressText: _CustomerConfigResource_Saving + "...",
                                                    width: 300,
                                                    wait: true,
                                                    waitConfig: { interval: 200 },
                                                    icon: 'download',
                                                    animEl: 'saving'
                                                });
                                                form.getForm().submit({
                                                    url: "HandlerADUTrackerPolygonFence",
                                                    method: "POST",
                                                    success: function(form, action) {
                                                        var flag = action.result.success;
                                                        if (flag == true) {
                                                            Ext.MessageBox.hide();
                                                            msgUpdateSuccess();
                                                            pgeofencewindow.hide();
                                                            pgeofencestore.reload({
                                                                params: {
                                                                    namekey: ''
                                                                }
                                                            });
                                                        }
                                                    },
                                                    failure: function(form, action) {
                                                        msgUpdateFailure();
                                                    },
                                                    params: {
                                                        snids: trackerCombo.getValue(),
                                                        fenceids: fenceCombo.getValue(),
                                                        tracker_polygon_fence_relation_id: grid.getSelectionModel().getSelections()[0].get('tracker_polygon_fence_relation_id'),
                                                        type: 'edit'
                                                    }
                                                });
                                            }
                                        }
}]
                                    });
                                }
                                pgeofencewindow.show();
                            }
                            else {
                                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                                Ext.Msg.show({
                                    title: _TrackerMain_Title,
                                    msg: _POI_SelectOne,
                                    buttons: Ext.Msg.OK,
                                    animEl: 'elId',
                                    icon: Ext.MessageBox.INFO
                                });
                            }
                        }

                    }, {
                        xtype: 'tbseparator', width: 5
                    }, {
                        icon: 'images/delete.png',
                        handler: function() {
                            if (grid.getSelectionModel().getCount() == 1) {
                                Ext.MessageBox.YESNO = { yes: _TrackerMain_Yes, no: _TrackerMain_No };
                                Ext.MessageBox.confirm(_TrackerMain_Title, _Photo_SureToDelete, function(btn) {
                                    if (btn == "yes") {
                                        var id = grid.getSelectionModel().getSelections()[0].data.tracker_polygon_fence_relation_id;
                                        Ext.Ajax.request({
                                            url: 'HandlerADUTrackerPolygonFence',
                                            method: 'POST',
                                            success: function(a, action) {
                                                var flag = Ext.util.JSON.decode(a.responseText).success;
                                                if (flag == true) {
                                                    Ext.MessageBox.hide();
                                                    msgUpdateSuccess();
                                                    pgeofencestore.reload({
                                                        params: {
                                                            namekey: ''
                                                        }
                                                    });
                                                }
                                                else {
                                                    msgWarning(_PictureResource_Deletefailed);
                                                }
                                            },
                                            failure: function() {
                                                msgWarning(_PictureResource_Deletefailed);
                                            },
                                            params: {
                                                ids: id,
                                                type: 'delete'
                                            }
                                        });
                                    }
                                }, this);
                            }
                            else {
                                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                                Ext.Msg.show({
                                    title: _TrackerMain_Title,
                                    msg: _POI_SelectAtLeastOne,
                                    buttons: Ext.Msg.OK,
                                    animEl: 'elId',
                                    icon: Ext.MessageBox.INFO
                                });
                            }
                        }
                    }

        ]
    });

    var grid = new Ext.grid.GridPanel({
        store: pgeofencestore,
        sm: new Ext.grid.CheckboxSelectionModel({ dataIndex: "tracker_polygon_fence_relation_id" }),
        viewConfig: {
            forceFit: true
        },
        enableHdMenu: false,
        autoScroll: true,
        columns: [
                { header: _ImageButtonText_TrackerName, width: 120, sortable: true, menuDisabled: true, dataIndex: 'tracker_name' },
                { header: _TxtGeofenceName, width: 120, sortable: true, menuDisabled: true, dataIndex: 'name' },
                { header: _Alarm_Header_AlarmType, width: 120, sortable: true, menuDisabled: true, dataIndex: 'alarm_type', renderer: function(value) {
                    return getPolygonAlarmTypeByIndex(value);
                }
                }
            ],
        width: 420,
        height: 200

    });

    grid.addListener('rowdblclick', function(obj, index) {
        var record = grid.getStore().getAt(index);
        //显示多边形
        var iiid = record.get('polygon_geofence_id');
        imap.showCannotEditPolygonFence(record.get('points'), iiid, true, record.get('name'));
    });

    var pgeofencewindow = new Ext.Window({
    title: _TxtTrackerPolygonFenceReport,
        id: 'tracker_polygon_fence_relation_window',
        iconCls: 'icon-report-polygon-fence-tracker',
        layout: 'fit',
        height: 250,
        width: 480,
        items: grid,
        tbar: tbar,
        minimizable: true,
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(pgeofencewindow);
            }
        }
    });

    pgeofencewindow.show();
}